<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.taskName"
          clearable
          size="small"
          placeholder="请输入任务名称"
          style="width: 200px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation />
    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="processInstanceName" label="流程实例名称" />
      <el-table-column prop="name" label="任务名称" />
      <el-table-column prop="createTime" label="开始时间">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="dueDate" label="到期日期">
        <template slot-scope="scope">
          <span>{{ scope.row.dueDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="owner" label="所有人" />
      <el-table-column prop="assignee" label="执行人" />
      <el-table-column
        v-permission="['admin', 'task:edit', 'task:del']"
        label="操作"
        width="200px"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="info(scope.row.processInstanceId)"
          >详情
          </el-button>
          <el-button
            v-if="
              scope.row.endTime === null &&
                (scope.row.assignee === null || scope.row.assignee === '')
            "
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="claim(scope.row)"
          >认领
          </el-button>
          <el-button
            v-if="
              scope.row.endTime === null &&
                scope.row.assignee === $store.getters.user.id + ''
            "
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="unclaim(scope.row)"
          >取消认领
          </el-button>
          <el-button
            v-if="
              scope.row.endTime === null &&
                scope.row.assignee !== null &&
                scope.row.assignee === $store.getters.user.id + ''
            "
            v-permission="['admin', 'task:edit']"
            style="margin-left: 3px"
            type="text"
            size="mini"
            @click="execute(scope.row)"
          >执行
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
    <!--详情-->
    <process-detail
      v-if="dialogViewVisible"
      :visible.sync="dialogViewVisible"
      :process-instance-id="processInstanceId"
    />
    <!-- 执行任务 -->
    <execute-task
      v-if="dialogExecuteTaskVisible"
      :visible.sync="dialogExecuteTaskVisible"
      :execute-task-id="executeTaskId"
      @executeTaskFinished="crud.toQuery()"
    />
  </div>
</template>

<script>
import CRUD, { presenter, header } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import crudTask from '@/api/flowable/task'
import processDetail from '../components/ProcessDetail'
import executeTask from '../components/ExecuteTask'

export default {
  name: 'FlowableTaskTodo',
  components: { executeTask, processDetail, pagination, crudOperation, rrOperation },
  cruds() {
    return CRUD({ title: '我的待办', url: 'api/flowable/task/todo' })
  },
  mixins: [presenter(), header()],
  data() {
    return {
      dialogViewVisible: false,
      dialogExecuteTaskVisible: false,
      processInstanceId: '',
      executeTaskId: '',
      permission: {
        add: ['admin', 'task:add'],
        edit: ['admin', 'task:edit'],
        del: ['admin', 'task:del']
      }
    }
  },
  created() {
    this.crud.optShow = {
      add: false,
      edit: false,
      del: false,
      download: false
    }
  },
  methods: {
    // 详情
    info(processInstanceId) {
      this.processInstanceId = processInstanceId
      this.dialogViewVisible = true
    },
    // 认领
    claim(row) {
      crudTask.claim(row).then(res => {
        this.crud.dleChangePage(1)
        this.crud.notify('认领成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    // 取消认领
    unclaim(row) {
      crudTask.unclaim(row).then(res => {
        this.crud.dleChangePage(1)
        this.crud.notify('取消认领成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    // 执行
    execute(row) {
      this.executeTaskId = row.id
      this.dialogExecuteTaskVisible = true
    }
  }
}
</script>
